;(function($){
	$.fn.present_reason = function(opts){
		var el = this;
		var opt = {
			no : "",
			reason : "",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		var t = this;
		t.initHtml();
		t.getPresentReason();
		t.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//返回
			el.find("#modalBtnBack").click(function(){
				param.closeBack();
				el.modal("hide");
				el.empty();
			})
			
			//确定
			el.find("#modalBtnCheck").click(function(){
				if(param.reason){
					param.dataBack(param.reason)
					el.modal("hide");
					el.empty();
				}else{
					jf_alert("请选择原因")
				}
			})
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
    		var html=	'<div class="modal-dialog" style="width:600px;">'+
				    		'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">	'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
					  			'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="modal-h4">'+$.i18n.propJsp("赠送原因")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+
										
									'<div class="input-group modal-remark" id="coupon_remark">'+
										'<span class="input-group-addon" style="background: white;border: 0px;">'+
											$.i18n.propJsp("原因：")+
										'</span>'+
										'<textarea class="form-control search-query" placeholder="" id="reason" name="reason"></textarea>'+
//										'<span class="input-group-btn">'+
//											'<button type="button" class="btn btn-primary btn-sm">'+
//												'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
//												$.i18n.propJsp("选择")+
//											'</button>'+
//										'</span>'+
									'</div>'+
									'<div id="commScale" class="widget-box transparent" style="margin:2px 12px;display:block;margin-top:20px;">'+
									
									'</div>'+
								'</div>'+				
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<button id="modalBtnCheck" class="btn btn-info btn-sm data-dismiss="modal">'+
												'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
											'</button>'+
											'<button id="modalBtnBack" class="btn btn-default btn-sm" data-dismiss="modal">'+
												'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+
											'</button>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
    		el.html(html);
		},
		getPresentReason : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			t.wait()
			var arg = {"arg":{"no":param.no}}
			$.ajax({
    			url: $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
    			type: "post",
    			dataType: "json",
    			data: $.HSAPI_ASKJSONINFO(JSON.stringify(arg),'POSDiscountDefault',"{}"),
    			success : function(data){
    				t.goon();
					if(data.RETURN == 1){
						var html = "";
						$(data.discodetype).each(function(k,v){
							html += '<button type="button" class="btn btn-app btn-light btnRate" data-code="'+v.code.trim()+'" style="font-size: 14px; width: 131px; height: 44.5px;">'+
										v.descript1+
									'</button>';
						})
						el.find("#commScale").html(html);
						el.find(".btnRate").unbind("click").click(function(){
							el.find("#reason").val($(this).html())
							param.reason = $(this).data("code");
						})
    				}else{
    					jf_alert(data.MSG);
    				}
    			},
    			error : function(data){
    					t.goon();
        			jf_alert(JSON.stringify(data));
    			},
    		})
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		}
	}
	
})(jQuery)